<?php
class Khachhang_Model_Khcn extends Zend_Db_Table{
	
	protected $_name = 'khachhang_cn';
	protected $_primary ='id';
	
	protected $_ids;
	
	public function getItem($arrParam = null, $options = null) {
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$select = $db	->	select()
							->	from('khachhang AS kh')
							->	joinLeft('khachhang_cn AS khcn', 'khcn.id = kh.id', array('*', "DATE_FORMAT(khcn.birthday, '%d/%m/%Y') AS birthday", "DATE_FORMAT(khcn.ngaycap, '%d/%m/%Y') AS ngaycap"))
							->	joinLeft('users AS u', 'kh.id_nhanvien = u.id', array('id AS user_id'))
							->	joinLeft('phongban AS pb', 'u.id_phongban = pb.id', array('id AS phongban_id'))
							->	where('kh.id = ?', $arrParam['id']);
			$result = $db	->	fetchRow($select);
		}
		
		return $result;
	}
	
	public function saveItem($arrParam = null, $options = null) {
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		if($options['task'] == 'admin-add') {
			
			$info 					= new Zendvn_System_Info();
			$created_by 			= $info->getMemberInfo('id');

			$status_tiemnang = 0;
			if (isset($arrParam['status_tiemnang'])) {
				$status_tiemnang = $arrParam['status_tiemnang'];
			}
			
			$id_marketing = '';
			if (count($arrParam['id_marketing']) > 0) {
				$id_marketing = implode(',', $arrParam['id_marketing']);
			}
			
			$id_khoahoc = '';
			if (count($arrParam['id_khoahoc']) > 0) {
				$id_khoahoc = implode(',', $arrParam['id_khoahoc']);
			}
			
			$id_manager_different = '';
			if (count($arrParam['id_manager_different']) > 0) {
				$id_manager_different = implode(',', $arrParam['id_manager_different']);
			}
			
			$bind	= array(
						'name' 					=> stripslashes($arrParam['name']),
						'email' 				=> $arrParam['email'],
						'phone' 				=> $arrParam['phone'],
						'telephone' 			=> $arrParam['telephone'],
						'fax' 					=> $arrParam['fax'],
						'address' 				=> stripslashes($arrParam['address']),
						'note' 					=> $arrParam['note'],
						'created' 				=> @date("Y-m-d H:i:s"),
						'created_by' 			=> $created_by,
						'modified' 				=> @date("Y-m-d H:i:s"),
						'modified_by' 			=> $created_by,
						'status_tiemnang' 		=> $status_tiemnang,
						'id_cat' 				=> 1,
						'id_nguon' 				=> $arrParam['id_nguon'],
						'id_nguon_dk' 			=> $arrParam['id_nguon_dk'],
						'id_nhanvien' 			=> $arrParam['id_nhanvien'],
						'id_phongban' 			=> $arrParam['id_phongban'],
						'id_manager_different' 	=> $id_manager_different,
						'id_district' 			=> $arrParam['id_district'],
						'id_city' 				=> $arrParam['id_city'],
						'id_country' 			=> $arrParam['id_country'],
						'id_marketing' 			=> $id_marketing,
						'id_khoahoc' 			=> $id_khoahoc
					);
			
			$table 	= 'khachhang';
			$db->insert($table, $bind);
			$lastId = $db->lastInsertId();
			
			// Them vao bang ca nhan
			$date     = new Zend_Date();
			if (!empty($arrParam['birthday'])) {
				$date->set($arrParam['birthday']);
			} else {
				$date->set('00/00/0000');
			}
			$birthday    = $date->toString('YYYY-MM-dd');
			
			if (!empty($arrParam['ngaycap'])) {
				$date->set($arrParam['ngaycap']);
			} else {
				$date->set('00/00/0000');
			}
			$ngaycap    = $date->toString('YYYY-MM-dd');
				
			$bind	= array(
					'id'					=> $lastId,
					'id_hocvan' 			=> $arrParam['id_hocvan'],
					'id_truonghoc' 			=> $arrParam['id_truonghoc'],
					'id_tinhtrang_laodong' 	=> $arrParam['id_tinhtrang_laodong'],
					'id_nghenghiep' 		=> $arrParam['id_nghenghiep'],
					'id_vaitro' 			=> $arrParam['id_vaitro'],
					'id_mucsong' 			=> $arrParam['id_mucsong'],
					'id_quymogiadinh' 		=> $arrParam['id_quymogiadinh'],
					'id_quoctich' 			=> $arrParam['id_quoctich'],
					'id_tongiao' 			=> $arrParam['id_tongiao'],
					'gender' 				=> $arrParam['gender'],
					'tinhtranghonnhan' 		=> $arrParam['tinhtranghonnhan'],
					'sothich' 				=> stripslashes($arrParam['sothich']),
					'ghichu' 				=> stripslashes($arrParam['ghichu']),
					'cmnd'	 				=> $arrParam['cmnd'],
					'ngaycap'	 			=> $ngaycap,
					'noicap'	 			=> $arrParam['noicap'],
					'birthday'	 			=> $birthday,
					'yahoo'	 				=> $arrParam['yahoo'],
					'skype'	 				=> $arrParam['skype'],
					'noilamviec'	 		=> $arrParam['noilamviec'],
					'phongban' 				=> $arrParam['phongban'],
					'chucvu'	 			=> $arrParam['chucvu']
			);
				
			$table 	= 'khachhang_cn';
			$db->insert($table, $bind);
			
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Thêm khách hàng cá nhân mới - id ' . $lastId,
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
	
		if($options['task'] == 'admin-edit'){
			$info 				= new Zendvn_System_Info();
			$id_user 			= $info->getMemberInfo('id');

			$status_tiemnang = 0;
			if (isset($arrParam['status_tiemnang'])) {
				$status_tiemnang = $arrParam['status_tiemnang'];
			}
			
			$id_marketing = '';
			if (count($arrParam['id_marketing']) > 0) {
				$id_marketing = implode(',', $arrParam['id_marketing']);
			}
			
			$id_khoahoc = '';
			if (count($arrParam['id_khoahoc']) > 0) {
				$id_khoahoc = implode(',', $arrParam['id_khoahoc']);
			}
			
			$id_manager_different = '';
			if (count($arrParam['id_manager_different']) > 0) {
				$id_manager_different = implode(',', $arrParam['id_manager_different']);
			}
			
			$bind	= array(
						'name' 					=> stripslashes($arrParam['name']),
						'email' 				=> $arrParam['email'],
						'phone' 				=> $arrParam['phone'],
						'telephone' 			=> $arrParam['telephone'],
						'fax' 					=> $arrParam['fax'],
						'address' 				=> stripslashes($arrParam['address']),
						'note' 					=> $arrParam['note'],
						'modified' 				=> @date("Y-m-d H:i:s"),
						'modified_by' 			=> $id_user,
						'status_tiemnang' 		=> $status_tiemnang,
						'id_cat' 				=> 1,
						'id_nguon' 				=> $arrParam['id_nguon'],
						'id_nguon_dk' 			=> $arrParam['id_nguon_dk'],
						'id_nhanvien' 			=> $arrParam['id_nhanvien'],
						'id_phongban' 			=> $arrParam['id_phongban'],
						'id_manager_different' 	=> $id_manager_different,
						'id_district' 			=> $arrParam['id_district'],
						'id_city' 				=> $arrParam['id_city'],
						'id_country' 			=> $arrParam['id_country'],
						'id_marketing' 			=> $id_marketing,
						'id_khoahoc' 			=> $id_khoahoc
					);
			$where = ' id = ' . $arrParam['id'];
			$table 	= 'khachhang';
			$db->update($table, $bind, $where);
			$lastId = $arrParam['id'];
			
			// Them vao bang ca nhan
			$date     = new Zend_Date();
			if (!empty($arrParam['birthday'])) {
				$date->set($arrParam['birthday']);
			} else {
				$date->set('00/00/0000');
			}
			$birthday    = $date->toString('YYYY-MM-dd');
			
			if (!empty($arrParam['ngaycap'])) {
				$date->set($arrParam['ngaycap']);
			} else {
				$date->set('00/00/0000');
			}
			$ngaycap    = $date->toString('YYYY-MM-dd');
				
			$bind	= array(
					'id_hocvan' 			=> $arrParam['id_hocvan'],
					'id_truonghoc' 			=> $arrParam['id_truonghoc'],
					'id_tinhtrang_laodong' 	=> $arrParam['id_tinhtrang_laodong'],
					'id_nghenghiep' 		=> $arrParam['id_nghenghiep'],
					'id_vaitro' 			=> $arrParam['id_vaitro'],
					'id_mucsong' 			=> $arrParam['id_mucsong'],
					'id_quymogiadinh' 		=> $arrParam['id_quymogiadinh'],
					'id_quoctich' 			=> $arrParam['id_quoctich'],
					'id_tongiao' 			=> $arrParam['id_tongiao'],
					'gender' 				=> $arrParam['gender'],
					'tinhtranghonnhan' 		=> $arrParam['tinhtranghonnhan'],
					'sothich' 				=> stripslashes($arrParam['sothich']),
					'ghichu' 				=> stripslashes($arrParam['ghichu']),
					'cmnd'	 				=> $arrParam['cmnd'],
					'ngaycap'	 			=> $ngaycap,
					'noicap'	 			=> $arrParam['noicap'],
					'birthday'	 			=> $birthday,
					'yahoo'	 				=> $arrParam['yahoo'],
					'skype'	 				=> $arrParam['skype'],
					'noilamviec'	 		=> $arrParam['noilamviec'],
					'phongban' 				=> $arrParam['phongban'],
					'chucvu'	 			=> $arrParam['chucvu']
			);
			$where = ' id = ' . $arrParam['id'];
			$table 	= 'khachhang_cn';
			$db->update($table, $bind, $where);
			
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Sửa khách hàng cá nhân - id ' . $lastId,
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
		
		return $lastId;
	}

}